package com.example.flutter_native_map

import android.annotation.TargetApi
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.webkit.ValueCallback
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import com.amap.api.maps.IAMapJsCallback
import com.amap.api.maps.IAMapWebView


class MAWebViewWrapper(private val webView: WebView?) : IAMapWebView {
    private var mapWebViewClient: WebViewClient? = null

    init {
        if (this.webView != null) {
            webView.webViewClient = object : WebViewClient() {
                @TargetApi(Build.VERSION_CODES.N)
                override fun shouldOverrideUrlLoading(
                    view: WebView,
                    request: WebResourceRequest
                ): Boolean {
                    if (mapWebViewClient != null) {
                        val flag = mapWebViewClient!!.shouldOverrideUrlLoading(view, request)
                        if (flag) {
                            return true
                        }
                    }
                    return super.shouldOverrideUrlLoading(view, request)
                }

                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                override fun shouldInterceptRequest(
                    view: WebView,
                    request: WebResourceRequest
                ): WebResourceResponse? {
                    if (mapWebViewClient != null) {
                        val flag = mapWebViewClient!!.shouldInterceptRequest(view, request)
                        if (flag != null) {
                            return flag
                        }
                    }
                    return super.shouldInterceptRequest(view, request)
                }

                override fun shouldInterceptRequest(
                    view: WebView,
                    url: String
                ): WebResourceResponse? {
                    if (mapWebViewClient != null) {
                        val flag = mapWebViewClient!!.shouldInterceptRequest(view, url)
                        if (flag != null) {
                            return flag
                        }
                    }
                    return super.shouldInterceptRequest(view, url)
                }
            }
        }
    }

    override fun evaluateJavascript(jsCallSig: String, callback: ValueCallback<String>) {
        if (this.webView != null) {
            webView.evaluateJavascript(jsCallSig, callback)
        }
    }

    override fun loadUrl(toString: String) {
        if (this.webView != null) {
            webView.loadUrl(toString)
        }
    }

    override fun addAMapJavascriptInterface(
        `object`: IAMapJsCallback,
        javascriptInterfaceName: String
    ) {
        if (this.webView != null) {
            webView.addJavascriptInterface(`object`, javascriptInterfaceName)
        }
    }

    override fun setWebViewClient(webViewClient: WebViewClient) {
        this.mapWebViewClient = webViewClient
    }

    override fun getWidth(): Int {
        if (this.webView != null) {
            return webView.width
        }
        return 0
    }

    override fun getHeight(): Int {
        if (this.webView != null) {
            webView.height
        }
        return 0
    }


    override fun addView(v: View, params: ViewGroup.LayoutParams) {
        if (webView != null && v != null) {
            webView.addView(v, params)
        }
    }
}